/**
 * @author Tsai, Min Che
 */
var loading;
$(function() {
	var progress = {
		report: function(percent) {
			percent = parseInt(percent) + $('#init_progress').progressbar('option', 'value');
			$('#init_progress').progressbar('option', 'value', percent);
		}
	}
	loading = {
		timer: null,
		count: 1,
		start: function() {
			if (this.timer == null) {
				this.timer = setInterval('loading.start()', 50);
			}
			if (this.count <= 100) {
				if (this.count % 3 == 1) {
					progress.report(4);
					this.count += 4;
				} else if (this.count % 7 == 3) {
					progress.report(8);
					this.count += 8;
				} else {
					progress.report(1);
					this.count++;
				}
			} else {
				clearInterval(loading.timer);
				
			}
		}
	}
	$('#loading').css({position: 'absolute', left: ($('body').width() - $('#loading').width()) / 2, top: 120});
	$('#init_progress').progressbar({
		value: 0,
		change: function(e, self) {
			value = $(this).progressbar('option', 'value');
			if (value >= 100) {
				$('#workbar').show();
				$('#loading').hide();
				$('#kumomado').show();
				$('#accordion').accordion();
			}
		}
	});
	loading.start();
	$('#start_button').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	).click(
		function() {
			var workbar_pos = $('#workbar').position();
			var height = $('#accordion').height();
			var workbar_height = $('#workbar').height();
			var top = workbar_pos.top - height - workbar_height;
			$('#accordion').css({
				left: 0,
				top: top + 'px'
			});
		}	
	);
	$('#workspace').click(
		function() {
			$('#accordion').css({left: '-' + $('#accordion').width() + 'px'});
		}
	);
	$('#accordion > div > a').hover(
		function() {	
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	).click(
		function() {
			$('#accordion').css({left: '-' + $('#accordion').width() + 'px'});
			if (typeof $(this).attr('app') != 'undefined') {
				$.platform.app({
					app: $(this).attr('app'),
					sub_app: $(this).attr('sub_app'),
					method: $(this).attr('method')
				});
			}
		}
	);
});